﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace MvcApplicationYoung.Models
{
    public class MyDBDirect
    {
       public  MyDBDirect()
        {

        }

        public bool CheckUser(string userName,string password)
        {
            string connectionString =
               "Data Source=(local);Initial Catalog=mydb;"
               + "Integrated Security=false;uid=sa;pwd=197839yj";
            string queryString = "SELECT * from users";
            string returnString = String.Empty;

            DataTable dtable;
            using (SqlConnection connection =
                new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand(queryString, connection);
                try
                {
                    connection.Open();
                    SqlDataAdapter adpt = new SqlDataAdapter();
                    adpt.SelectCommand = command;
                    dtable = new DataTable();
                    adpt.Fill(dtable);
                    if (CheckUserInUsersTable(dtable, userName,password)) return true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            return false;
        }

        private bool CheckUserInUsersTable(DataTable dtable, string userName, string password)
        { 
            int rowCount=dtable.Select(string.Format("userName='{0}' and password='{1}'",userName,password)).Length;

            if (rowCount>0)
            {
                return true;
            }
        return false;
        }
    }
}
